package com.sky.directdeal.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.sky.directdeal.R;
import com.sky.directdeal.activity.StoreOffersListActivity;
import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.manager.ImageLoader;
import com.sky.directdeal.widget.DDProgressDialog;
import com.sky.local_ads.store_list.StoreDetails;


public class BusinessListAdapter extends BaseAdapter {

	private final Activity context;
	
	private List<StoreDetails> storeList;
	
	private boolean isService=false;
	
	private DDProgressDialog progressDialog;
	
	private final ImageLoader imageLoader;

	public BusinessListAdapter(Activity context,
			List<StoreDetails> storeArrayList) {
		this.context = context;
		storeList = storeArrayList;
		imageLoader = new ImageLoader(context, R.drawable.loading_2x2, R.drawable.noimage_2x2);
	}

	@Override
	public int getCount() {
		if (storeList != null && storeList.size() > 0) {
			return storeList.size();
		}
		return 0;

	}

	@Override
	public Object getItem(int position) {
		return storeList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return storeList.indexOf(getItem(position));
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (convertView == null) {

			convertView = inflater.inflate(R.layout.store_list_row,
					parent, false);
			holder = new ViewHolder();
			holder.businessName = (TextView) convertView
					.findViewById(R.id.storeNameText);
			holder.businessAddress=(TextView)convertView.findViewById(R.id.textViewAddress);
			holder.businessPhNo=(TextView)convertView.findViewById(R.id.textViewPhoneNo);
			holder.businessProfileImageView=(ImageView)convertView.findViewById(R.id.imageViewBusiness);
			//holder.businessRating=(RatingBar)convertView.findViewById(R.id.storeRatingBar);
			convertView.setTag(holder);

		} else {

			holder = (ViewHolder) convertView.getTag();

		}

		final StoreDetails storeDetails = (StoreDetails) getItem(position);
		
		if(storeDetails!=null) {
			String storeName="LOCAL STORE";
			
			if(storeDetails.getBusinessName()!=null) {
				storeName=storeDetails.getBusinessName();
			}
			holder.businessName.setText(storeName);
			holder.businessAddress.setText(storeDetails.getAddress());
			holder.businessPhNo.setText(storeDetails.getPhoneNumber());
			float rating=storeDetails.getRating();
			//holder.businessRating.setRating(rating);
			imageLoader.displayImage("http://www.filmyfriday.com/wp-content/uploads/2014/01/86748-thumb.jpg", holder.businessProfileImageView);
			
		}
		
		Button businessOfferButton=(Button)convertView.findViewById(R.id.businessOffer);
		businessOfferButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent storeOfferIntent=new Intent(context,StoreOffersListActivity.class);
				storeOfferIntent.putExtra(Constants.BUSINESS_DETAILS_INTENT, storeDetails);
				context.startActivity(storeOfferIntent);
			}
		});

		

		return convertView;
	}
	public void updateAdapter(List<StoreDetails> storeList) {
		this.storeList= storeList;
		notifyDataSetChanged();
	}
	private static class ViewHolder {

		private TextView businessName;
		private TextView businessAddress;
		private TextView businessPhNo;
		private ImageView businessProfileImageView;
		private RatingBar businessRating;
	}
	
	
	private void showProgressDialog() {
		if (progressDialog == null) {
			progressDialog = new DDProgressDialog(context,
					context.getString(R.string.please_wait),
					context.getString(R.string.loading), false, false);
			if (!context.isFinishing()) {
				progressDialog.show();
			}
			return;
		}

		if (!progressDialog.isShowing()) {
			if (!context.isFinishing()) {
				progressDialog.show();
			}
		}
	}
	

	private void dismissProgressDialog() {

		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
			progressDialog = null;
		}
	}

}
